﻿// 项目：	Guan.CMS.Web.Core - Guan.CMS
// 文件名：	SecurityExtensions.cs
// 
// 修改时间：2013-03-06 19:27
// 修改人：	Cooclsee

using System.Security.Principal;
using Guan.CMS.Web.Core.Models;

namespace Guan.CMS.Web.Core.Extensions
{
    public static class SecurityExtensions
    {
        public static string Name(this IPrincipal user)
        {
            return user.Identity.Name;
        }

        public static bool InAnyRole(this IPrincipal user, params string[] roles)
        {
            foreach (string role in roles)
            {
                if (user.IsInRole(role))
                {
                    return true;
                }
            }
            return false;
        }

        public static CmsUser GetCmsUser(this IPrincipal principal)
        {
            if (principal.Identity is CmsUser)
            {
                return (CmsUser) principal.Identity;
            }
            else
            {
                return new CmsUser(string.Empty, new UserInfo());
            }
        }
    }
}